package com.gongye.courseselectionsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gongye.courseselectionsystem.common.Check;
import com.gongye.courseselectionsystem.constants.ResultCodeEnum;
import com.gongye.courseselectionsystem.pojo.vo.ResultList;
import com.gongye.courseselectionsystem.mapper.ChooseCourseMapper;
import com.gongye.courseselectionsystem.pojo.po.ChooseCourse;
import com.gongye.courseselectionsystem.service.RemoveCourseService;
import com.gongye.courseselectionsystem.common.TimeJudge;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author baorongchuan
 */
@Service
@Slf4j
public class RemoveCourseServiceImpl implements RemoveCourseService {
    @Resource
    private Check check;
    @Resource
    private ChooseCourseMapper chooseCourseMapper;

    @Override
    public ResultList deleteChooseCourse(Integer studentNumber, Integer teacherJobNumber) {
        if (!TimeJudge.time()) {
            log.info("您不在选课时间内");
            return new ResultList(ResultCodeEnum.FAILED, "您不在选课时间内");
        }
        try {
            check.isExistNum(studentNumber, teacherJobNumber);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultList(ResultCodeEnum.FAILED, e.getMessage());
        }

        LambdaQueryWrapper<ChooseCourse> chooseCourseWrapper = new LambdaQueryWrapper<>();
        chooseCourseWrapper.eq(ChooseCourse::getStudentNumber, studentNumber);
        chooseCourseWrapper.eq(ChooseCourse::getTeacherJobNumber, teacherJobNumber);
        int num = chooseCourseMapper.delete(chooseCourseWrapper);
        if (num != 1) {
            return new ResultList(ResultCodeEnum.FAILED, "对不起，删除失败。请确认您是否选了工号为：【 " + teacherJobNumber + " 】老师的课程");
        }
        return new ResultList(ResultCodeEnum.SUCCESS, "取消选课成功");

    }
}
